require([
  "jquery",
  "ko",
  "bootstrap",
  "jquery-select2",
  "jquery-slimscroll",
  "jquery-gritter",
  "jquery-blockui",
  "bootstrap-datepicker",
  "bootstrap-datetimepickerlocal",
  "akcy"
], function($, ko) {
  var akcy = arguments[arguments.length - 1];
  akcy.ui.init();

  var table = akcy.table().options({
    headers: [
      {
        text: '服务名称',
        field: 'U_LoginName',
        width: '10%'
      }, {
        text: '主机',
        field: 'U_Name',
        width: '11%'
      }, {
        text: '端口',
        field: 'U_Gender',
        width: '5%'
      }, {
        text: '备注',
        field: 'U_Mobile',
        width: '11%'
      }
    ],
    // defaultOrderBy: "U_ID",
    dataQueryUrl: 'EtlCon/sysConfig/All'
  }).optionsExtend({
    　appId:"etl",
    branches: ko.observableArray([]),
    nodes: ko.observableArray([]),
    adduser: function() {
      form.getInstance().reset();
      akcy.showmodal("modal-user");
    },
    edituser: function(data) {
      actionTable.getInstance().etlid(pageForm.getInstance().id());
      actionTable.load();
    },
    isCheckedAll: ko.observable(false),
    checkedAll: function() {
      var array = [];
      if (table.getInstance().isCheckedAll() == false) {
        table.getInstance().isCheckedAll(true);
        $(table.getInstance().recordSet()).each(function() {
          array.push($(this)[0].id);
        });
      } else {
        table.getInstance().isCheckedAll(false);
      }
      table.getInstance().checkedUser(array);
    },
    checkedUser: ko.observableArray(),
    checkService: function(data) {
      if (!table.getInstance().checkedUser().contains(data.id))
        table.getInstance().checkedUser().insert(0, data.id);
      }
    ,
    rowclick: function(data) {
      pageForm.getInstance().reset();
      pageForm.load(data);
      actionTable.getInstance().etlid(data.id);
      actionTable.load();
    },
    exece: function(data) {
      prameTable.getInstance().etlid(data.id);
      prameTable.getInstance().serviceData(data);
      prameTable.load();
      akcy.showmodal("modal-user");
    },
    addService: function() {
      pageForm.getInstance().reset();
    },
    deleteService: function() {
      akcy.confirm('确认删除吗？', function() {
        akcy.ajaxdelete('EtlCon/sysConfig/DeleteConfig/' + table.getInstance().checkedUser().join(','), {}).done(function() {
          akcy.alert.success("删除成功！");
          table.load();
          prameTable.getInstance().etlid(0);
          prameTable.load();
        })
      })
    }
  }).bind("pagetree").load();
  var pageForm = akcy.form().options({
    fields: [
      {
        field: 'id',
        value: ko.observable(-1)
      }, {
        field: 'mainEngine',
        value: ko.observable()
      }, {
        field: 'parms',
        value: ko.observable()
      }, {
        field: 'path',
        value: ko.observable()
      }, {
        field: 'port',
        value: ko.observable()
      }, {
        field: 'remark',
        value: ko.observable()
      }, {
        field: 'serviceName',
        value: ko.observable()
      }, {
        field: 'type',
        value: ko.observable()
      }
    ],
    dataAddUrl: 'EtlCon/sysConfig/Add',
    dataUpdateUrl: 'EtlCon/sysConfig/Add',
    dataDeleteUrl: ''
  }).optionsExtend({　appId:"etl",
    typeList: ko.observableArray([
      {
        name: '天',
        value: '1'
      }, {
        name: '时间',
        value: '2'
      }
    ]),
    added: function() {
      table.load();
      pageForm.getInstance().reset();
    },
    updated: function() {
      table.load();
      akcy.alert.success("保存成功！");
    },
    deleted: function() {
      akcy.alert.success("删除成功！");
    }
  }).bind("pageinfo");

  var actionTable = akcy.edittable().options({
    fields: [
      {
        field: "etlid",
        value: ko.observable(),
        method: akcy.querymethod.equal
      }
    ],
    rowFields: function() {
      this.id = 0,
      this.paramName = "",
      this.paramType = "",
      this.remark = "",
      this.etlid = 0,
      this.paramCode = ""
    },
    dataQueryUrl: "EtlCon/sysConfig/Prames",
    dataAddUrl: "EtlCon/sysConfig/AddPrame",
    dataUpdateUrl: "EtlCon/sysConfig/AddPrame",
    dataDeleteUrl: "EtlCon/sysConfig/DeletePrame",
    isAsc: false,
    keyId: "id"
  }).optionsExtend({　appId:"etl",
  beforeUpdate:function(){



    
  },
    beforeAdd: function(data) {
      var pid = pageForm.getInstance().id();
      if (pid == -1 || pid == undefined) {
        akcy.alert.warning("请选择一项父级菜单");
        return false;
      } else {
        data.etlid = pid;
      }
    }
  }).bind("pageaction");
  var prameTable = akcy.table().options({
    fields: [
      {
        field: "etlid",
        value: ko.observable(),
        method: akcy.querymethod.equal
      }
    ],
    dataQueryUrl: "EtlCon/sysConfig/Prames"
  }).optionsExtend({　appId:"etl",
    searched: function(data) {
      akcy.ui.init();
    },
    beforeSearch: function() {
      prameTable.getInstance().showError(false);
    },
    serviceData: ko.observable(),
    showError: ko.observable(false),
    excuteEtl: function() {
      var $param = $(".param");
      var paramList = [];
      for (var i = 0; i < $param.length; i++) {
        paramList.push({
          paramCode: $($param[i]).attr("code"),
          paramValue: $($param[i]).val()
        });
      }
      akcy.ajaxpost('etl','EtlCon/sysConfig/excuteEtl', {
        etlConfigInfo: prameTable.getInstance().serviceData(),
        listParam: paramList
      }).done(function(rst) {
        if (rst) {
          akcy.alert.success("执行成功");
          prameTable.getInstance().showError(false);
        } else {
          prameTable.getInstance().showError(true);
        }
      })
    }
  }).bind("prameList");;
});
